#!/bin/bash

usage()
{
	echo ""
	echo "Usage: `basename $0` [options] [-jn] [-v] [-h]"
	echo "Options:"
	echo "  release, debug: "
	echo "      Specifies the build type."
	echo "  i386, amd64, arm, arm64, mips, mips64, ppc, ppc64:"
	echo "      Specifies the architecture for code generation."
	echo "  clean: "
	echo "      Clean the build folder."
	echo "  ci: "
	echo "      Specifies the environment is CI."
	echo "  -h, --help:"
	echo "      Print this message and exit."
	echo "  -j: enable make '-j' option."
	echo "      if 'n' is not given, will set jobs to auto detected core count, otherwise n is used."
	echo "  --use-clang:"
	echo "      Force use clang on Windows."
	echo ""
}

for i in "$@"
do
    case $i in
        i386|amd64|arm|arm64|armv6|mips|mips64|ppc|ppc64) BUILD_ARCH=$i
            ;;
        release|debug) BUILD_TYPE=$i
            ;;
        clean) CLEAN_BUILD="true"
            ;;
        ci) CI="ci"
            ;;
        -j*) BUILD_JOBS="${i#-j}"
            ;;
        --help|-h) usage
            exit 1
            ;;
        --use-clang)
            BUILD_USE_CLANG="true"
            ;;
        *) echo "illegal option $i"
            usage
            exit 1
            ;;
    esac
done

cmake -DBUILD_ARCH=${BUILD_ARCH}\
    -DBUILD_TYPE=${BUILD_TYPE}\
    -DCLEAN_BUILD=${CLEAN_BUILD}\
    -DBUILD_JOBS=${BUILD_JOBS}\
    -DBUILD_USE_CLANG=${BUILD_USE_CLANG}\
    -P build.cmake
